package com.ncell.cell;

import java.util.HashSet;
import java.util.List;

/**
 * @author Dmitry
 */
public class TemporalExtension {

    private List<PConnection> positiveList;
    private List<PConnection> negativeList;
    private HashSet<P> negativeListSet;
    private float activationCounter = 0;
    private float notActivationCounter = 0;

    @Override
    public String toString() {
        return "act=" + activationCounter + "; notAct=" + notActivationCounter + "; pos=" + positiveList.size() + "; neg=" + negativeList.size();
    }

    public List<PConnection> getPositiveList() {
        return positiveList;
    }

    public void setPositiveList(List<PConnection> positiveList) {
        this.positiveList = positiveList;
    }

    public List<PConnection> getNegativeList() {
        return negativeList;
    }

    public void setNegativeList(List<PConnection> negativeList) {
        this.negativeList = negativeList;
        if (negativeListSet == null) {
            negativeListSet = new HashSet<>(negativeList.size());
        } else {
            negativeListSet.clear();
        }

        for (PConnection c : negativeList) {
            negativeListSet.add(c.getP());
        }
    }

    public void addToNegtiveList(PConnection connection) {
        if (negativeListSet == null) {
            negativeListSet = new HashSet<>(negativeList.size());
        }

        negativeListSet.add(connection.getP());
        negativeList.add(connection);
    }

    public boolean containsNegative(P p) {
        return negativeListSet.contains(p);
    }

    public float getActivationCounter() {
        return activationCounter;
    }

    public void setActivationCounter(float activationCounter) {
        this.activationCounter = activationCounter;
    }

    public float getNotActivationCounter() {
        return notActivationCounter;
    }

    public void setNotActivationCounter(float notActivationCounter) {
        this.notActivationCounter = notActivationCounter;
    }

    public void setAllPsConnectedToActiveI() {
        if (negativeList != null) {
            for (PConnection c : negativeList) {
                c.getP().setConnectedToActive(true);
            }
        }

        if (positiveList != null) {
            for (PConnection c : getPositiveList()) {
                c.getP().setConnectedToActive(true);
            }
        }
    }

    public float countPositiveExtensionOarkn() {
        if (positiveList.isEmpty()) {
            return 0f;
        }

        int counter = 0;
        for (PConnection c : positiveList) {
            if (c.isPActive()) {
                counter++;
            }
        }

        return (counter / (float) positiveList.size()) * counter;
    }

    public float countNegativeExtensionActivation() {
        int counter = 0;
        for (PConnection c : negativeList) {
            if (!c.isPActive()) {
                counter++;
            }
        }

        return (counter / (float) negativeList.size()) * counter;
    }

    public void incrementActivationCounter() {
        activationCounter += 1.f;
        notActivationCounter *= 0.99f;
    }

    public void decrementActivationCounter() {
        activationCounter *= 0.99f;
        notActivationCounter += 1.f;
    }
}
